//
//  GHLoginViewController.m
//  GodHorses
//
//  Created by Mac on 2017/11/14.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "TMLoginViewController.h"
#import "UITextField+STInputLimit.h"
#import "TMForgotViewController.h"
#import "TMRegisterViewController.h"
#import "TMTabbarViewController.h"
#define textFiledBackgroundColor [UIColor colorWithRed:77/255.0 green:96/255.0 blue:129/255.0 alpha:1]
@interface TMLoginViewController ()
@property(nonatomic, strong) UITextField                     *accountTextFiled;
@property(nonatomic, strong) UITextField                     *pwdTextFiled;
@end

@implementation TMLoginViewController
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    [self configSubView];
    [self addNotifacations];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self st_hideNavagetionbarAnimated:NO];
    
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self st_showNavagationbarAnimated:NO];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
#pragma mark --Notifacation
- (void)addNotifacations{
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(textFieldTextDidChangeNotification:)
                                                 name:UITextFieldTextDidChangeNotification
                                               object:nil];
    
}
- (void)textFieldTextDidChangeNotification:(NSNotification*)notify{
    if (notify.object == self.pwdTextFiled) {
        [self.pwdTextFiled st_textInputLimitWithLength:16];
    }else if (notify.object == self.accountTextFiled){
        [self.accountTextFiled st_textInputLimitWithLength:11];
    }
}
- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark --subView
- (void)configSubView{
    self.view.backgroundColor = UIColor.whiteColor;
    [self st_autoAdjustAllResponder];
    
    self.accountTextFiled = [UITextField new];
    self.pwdTextFiled = [UITextField new];
    self.pwdTextFiled.secureTextEntry = YES;
    UIImageView * backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 172)];
    backImageView.image = [UIImage imageNamed:@"羽毛"];
    [self.view addSubview:backImageView];
    
    UIImageView * iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 95, 46)];
    iconImageView.image = [UIImage imageNamed:@"轻聊"];
    iconImageView.st_centerX = UIScreenWidth /2;
    iconImageView.st_top = 80;
    [self.view addSubview:iconImageView];
    
    
    
    UIView * accountView = [TMUtils normalTextField:self.accountTextFiled title:@"用户名/手机号"];
    accountView.st_top = 250;
    [self.view addSubview:accountView];
    
    UIView * pwdView = [TMUtils normalTextField:self.pwdTextFiled title:@"密码"];
    pwdView.st_top = accountView.st_bottom + 20;
    [self.view addSubview:pwdView];
    
    
    __weak typeof(self) weakSelf =  self;
    STButton * loginButton = [[STButton alloc] initWithFrame:CGRectMake(30, pwdView.st_bottom + 80, UIScreenWidth - 60, 44)
                                                       title:@"登录"
                                                  titleColor:[UIColor whiteColor]
                                                   titleFont:18
                                                cornerRadius:22
                                             backgroundColor:TM_ThemeBackGroundColor
                                             backgroundImage:nil
                                                       image:nil];
    loginButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [loginButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedLoginButton];
    }];
    [self.view addSubview:loginButton];
    
    
   
  
    STButton * autoLoginButton = [[STButton alloc] initWithFrame:CGRectMake(15, pwdView.bottom, 200, 44)
                                                           title:@"下次自动登录"
                                                      titleColor:SecendTextColor
                                                       titleFont:14
                                                    cornerRadius:0
                                                 backgroundColor:nil
                                                 backgroundImage:nil
                                                           image:[UIImage imageNamed:@"xuanzhong"]];
   
    
    [self.view  addSubview:autoLoginButton];
    
    
    
    STButton * forgotButton = [[STButton alloc] initWithFrame:CGRectMake(30 + 22, loginButton.st_bottom + 20, 100, 44)
                                                        title:@"忘记密码?"
                                                   titleColor:SecendTextColor
                                                    titleFont:13
                                                 cornerRadius:0
                                              backgroundColor:[UIColor clearColor]
                                              backgroundImage:nil
                                                        image:nil];
    forgotButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    forgotButton.right = UIScreenWidth - 15;
    forgotButton.top = pwdView.bottom;
    [forgotButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedforgotButton];
    }];
    [self.view addSubview:forgotButton];
    
    STButton * registerButton = [[STButton alloc] initWithFrame:CGRectMake(30, loginButton.st_bottom + 20, UIScreenWidth, 44)
                                                          title:@"还没有账号,去注册"
                                                     titleColor:SecendTextColor
                                                      titleFont:13
                                                   cornerRadius:0
                                                backgroundColor:[UIColor clearColor]
                                                backgroundImage:nil
                                                          image:nil];
    registerButton.centerX = UIScreenWidth/2;
    registerButton.bottom = UIScreenHeight;
    registerButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [registerButton setAttributedTitle:[registerButton.currentTitle st_convertAttributeStringWithKeyWord:@"注册" attributes:@{NSForegroundColorAttributeName:TM_ThemeBackGroundColor}] forState:UIControlStateNormal];
    [registerButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedregisterButton];
    }];
    [self.view addSubview:registerButton];
    
    
}

#pragma mark --Action Method
- (void)onSelectedLoginButton{
    if (self.accountTextFiled.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入用户名/手机号"];
        return;
    }

    if (self.pwdTextFiled.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入密码"];
        return;
    }
    [SVProgressHUD showWithStatus:@"登录中"];
    [self loginWithMobie:self.accountTextFiled.text pwssWord:self.pwdTextFiled.text];
}
- (void)onSelectedforgotButton{
    [self.navigationController pushViewController:[TMForgotViewController new] animated:YES];
}
- (void)onSelectedregisterButton{
    
    [self.navigationController pushViewController:[TMRegisterViewController new] animated:YES];
    
}

#pragma mark --NetWork Method
- (void)loginWithMobie:(NSString *)mobile pwssWord:(NSString *)passWord{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * paramDic = [NSMutableDictionary new];
    if (mobile.length) {
        [paramDic setObject:mobile forKey:@"user_name"];
    }
    if (passWord.length) {
        [paramDic setObject:passWord forKey:@"password"];
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"/Rest/Login/login"];
    DDLogInfo(@"url:%@",url);
    DDLogInfo(@"param:%@",paramDic);
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:paramDic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 NSDictionary * modelDic = [responseObject valueForKey:@"data"];
                                                 DDLogInfo(@"%@请求成功:resp\n%@",url,responseObject);
                                                 MCUserModel * user = [MCUserModel mj_objectWithKeyValues:modelDic];
                                                 [[STUserManger defult] updateAccount:mobile pwd:passWord];
                                                 [[STUserManger defult] updateUserModel:user];
                                                 [TMUtils fecthUserModelWithUserId:user.userID.description handle:^(NSArray<MCUserModel *> *users) {
                                                     [[STUserManger defult] updateUserModel:users.firstObject];
                                                 }];
                                                 [SVProgressHUD showSuccessWithStatus:@"登录成功"];
                                                 
                                                 [UIApplication sharedApplication].keyWindow.rootViewController = [TMTabbarViewController new];
                                                 [[UIApplication sharedApplication].keyWindow st_showAnimationWithType:STAnimationTyperippleEffect];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 DDLogError(@"\nurl:%@\nparam:%@\nreson:%@",url,paramDic,error.desc);
                                             }];
}

@end

